<?php
/**
*
*	自定义sql缓存的设置 @zairwolf
*
*/
if(!defined('IN_ACP')) exit('Access Denied');

clearCache('cache_custom');

//新增缓存
if($action == 'insert') {
	$cache=_post('cache');
	$title=_post('title');

	if(empty($cache)) b('缓存标识不能为空');
	if(!preg_match('/^[A-Za-z0-9]+$/', $cache)) b('缓存标识只能使用字母和数字');
	if(db_r("select id from rd8_cache where id='$cache'")) b('缓存标识不能重复');

	db_i("insert into rd8_cache set id='$cache',title='$title'");

	header('Location: ?ptype='.$ptype.'&pfile='.$pfile.'&action=edit&id='.$cache);
	exit;
}

//编辑缓存
if($action == 'edit') {
	$id=_get('id');

	$cache = db_r("select * from rd8_cache where id='$id'");
	if(!$cache) b('未知缓存');

	$tpl = new T($pfile.'_edit');
	$tpl->assign($cache);
	$tpl->output();
}

//编辑更新
if($action == 'update') {
	$id=_post('id');
	$title=_post('title');
	$customsql=_post('customsql');
	$twod=_post('twod');
	$customgroup=_post('customgroup');
	$customwith=_post('customwith');

	$cols=array(
		'title' => $title,
		'csql'   => $customsql,
		'twod'  => $twod,
		'cgroup' => $customgroup,
		'cwith'  => $customwith,
	);
	db_q("update rd8_cache set ".sqlcol($cols)." where id='$id'");

	_cache_update('cache');
	j("?ptype=$ptype&pfile=$pfile", '编辑成功');
}

//锁定禁用
if($action == 'lock') {
	$id=_get('id');

	db_q("update rd8_cache set active=(active+1)%2 WHERE id='$id'");
	_cache_update('cache');
	j("?ptype=$ptype&pfile=$pfile", '状态修改成功');
}

//删除
if($action == 'delete') {
	$id=_get('id');

	db_q("delete from rd8_cache where id='$id'");
	_cache_update('cache');
	j("?ptype=$ptype&pfile=$pfile", '删除成功');
}

//默认显示
if(!$action){
	$caches = db_rows("select * from rd8_cache order by id desc");

	$tpl = new T($pfile);
	$tpl->assign('caches', $caches);
	$tpl->output();
}


